package tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.UUID;

import static org.junit.Assert.*;

import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;

import reit.main.RepairMaterial;
import reit.main.RepairObjectInterface;
import reit.main.RepairTool;
import reit.main.Warehouse;

import java.util.List;
 
public class WarehouseTDD {
	private Warehouse mWarehouse;
	private Logger logger;
	@Before public void setUp(){
		mWarehouse = new Warehouse();
		logger = Logger.getLogger(WarehouseTDD.class);
	}
	
	@Test
	public void testLogger() {
		// configurations, if file path changed, change in log4j.proprties
		String filePath = "..\\log.out";
		UUID uuid = UUID.randomUUID();
        String randomUUIDString = uuid.toString();
        String finalData = "JUnitLoggerTest - " + randomUUIDString;
		logger.info(finalData);
		File f = new File(filePath);
		
		// test if log file was created
		assertTrue(f.exists() && !f.isDirectory());
		
		// test if guid is found in file
		BufferedReader br;
		try {
			br = new BufferedReader(new FileReader(filePath));
			String line;
			boolean found=false;
			while ((line = br.readLine()) != null) {
			   if (line.indexOf(finalData) > -1){
				   found=true;
			   }
			}
			br.close();
			assertTrue(found);
		} catch (FileNotFoundException e) {
			fail("logger test failed "+ e.getMessage());
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			fail("logger test failed "+ e.getMessage());
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	@Test
	public void CheckgetCountTool() {
		/*
		 * 	test count of random tool and material in empty warehouse
		 */
		RepairTool RT = new RepairTool("poop",1);
		assertEquals(mWarehouse.getCount(RT),0);
		List<RepairObjectInterface> RTL = new ArrayList<RepairObjectInterface>();
		RTL.add(RT);
		mWarehouse.checkIn(RTL);
		assertEquals(1,mWarehouse.getCount(RT),0);
		
		//	do the same for materials
		RepairMaterial RM = new RepairMaterial("poop2",1);
		assertEquals(mWarehouse.getCount(RM),0);
		List<RepairObjectInterface> RML = new ArrayList<RepairObjectInterface>();
		RML.add(RM);
		mWarehouse.checkIn(RML);
		assertEquals(1,mWarehouse.getCount(RM),0);
	}
	
	@Test
	public void CheckInFunctionTest() {
		/*
		 * 	try to check out objects from an empty warehouse
		 */
		List<RepairObjectInterface> tRepairTools = new ArrayList<RepairObjectInterface>();
		RepairTool RT = new RepairTool("poop",5);
		tRepairTools.add(RT);		
		assertTrue(mWarehouse.checkIn(tRepairTools));
		assertEquals(mWarehouse.getCount(RT),5);
		
		//	do the same for materials
		List<RepairObjectInterface> tRepairMaterials = new ArrayList<RepairObjectInterface>();
		RepairMaterial RM = new RepairMaterial("poop2",4);
		tRepairMaterials.add(RM);		
		assertTrue(mWarehouse.checkIn(tRepairMaterials));
		assertEquals(mWarehouse.getCount(RM),4);
	}
	
	@Test
	public void CheckOutWarehouseFunctionTest() {
		/*
		 * 	try to check out objects from an empty warehouse
		 */
		List<RepairObjectInterface> tRepairTools = new ArrayList<RepairObjectInterface>();
		RepairTool RT = new RepairTool("poop",5);
		tRepairTools.add(RT);
		mWarehouse.checkOut(tRepairTools);
		assertEquals(mWarehouse.getCount(RT),0);
		assertTrue(mWarehouse.isEmpty());
		
		//	do the same for materials
		List<RepairObjectInterface> tRepairMaterials = new ArrayList<RepairObjectInterface>();
		RepairMaterial RM = new RepairMaterial("poop2",4);
		tRepairMaterials.add(RM);
		mWarehouse.checkOut(tRepairMaterials);
		assertEquals(mWarehouse.getCount(RM),0);
		assertTrue(mWarehouse.isEmpty());
		
		/*
		 * 	check in objects and then try to check out some of them 
		 */
		
		List<RepairObjectInterface> tRepairTools2 = new ArrayList<RepairObjectInterface>();
		RepairTool RT2 = new RepairTool("poop",4);
		tRepairTools2.add(RT2);
		mWarehouse.checkIn(tRepairTools);
		mWarehouse.checkOut(tRepairTools2);
		assertEquals(1,mWarehouse.getCount(RT),0);
		assertFalse(mWarehouse.isEmpty());
		
		//	do the same for materials
		List<RepairObjectInterface> tRepairMaterials2 = new ArrayList<RepairObjectInterface>();
		RepairMaterial RM2 = new RepairMaterial("poop2",2);
		tRepairMaterials2.add(RM2);
		mWarehouse.checkIn(tRepairMaterials);
		mWarehouse.checkOut(tRepairMaterials2);
		assertEquals(mWarehouse.getCount(RM),2);
		assertFalse(mWarehouse.isEmpty());		
	}
	//TODO:: concurrency tests
	


}
